/******************************************************************************
 * * call_log_global.h - data structure for call log module
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/
#ifndef CALL_LOG_GLOBAL_H
#define CALL_LOG_GLOBAL_H

#ifdef __cplusplus
extern "C" {
#endif

/**********************
*      INCLUDES
**********************/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "lvgl.h"
#include "ui_type.h"
#include "ui_textid.h"
#include "../../lvgl/hal/hal.h"
#include "modem/mmi_modemadp_interface.h"
#include "inter_common_ui_interface.h"
#include "inter_call_calllog.h"
#include "inter_call_framework_interface.h"
#include "inter_calllog_framework_interface.h"
#include "inter_sms_interface.h"
#include "inter_contacts_interface.h"
#include "inter_framework_interface.h"
#include "nav.h"
#include "ui_nvm_interface.h"
#include "utility.h"

#define CALL_LOG_LIST_CONT_CNT    2  // Container count in the list

/**********************
*      TYPEDEFS
**********************/

/* Node of CallLogList */
typedef struct {
    NV_Call_Log_Data_t Data;       /* call log data */
    UINT16             BlockId;    /* block ID in NVM */
} Call_Log_Node_t;

/* Call log management */
typedef struct {
    lv_ll_t         CallLogList;                              /* call log list, including all types, sorted by timestamp descendingly */
    UINT8           BlockBitmap[NV_CALL_LOG_MAX_NUM / 8 + 1]; /* one bit represents one block in NVM. 1: allocated, 0: free */

    UINT16          MissedCalls;                              /* count of missed calls */
    UINT16          ReceivedCalls;                            /* count of received calls */
    UINT16          DialledCalls;                             /* count of dialled calls */
    UINT8           UnreadMissedCalls;                        /* count of unread missed calls */

    CALL_LOG_TYPE   CurrType;                                 /* current call log type */
    VOID            *CurrDesc;                                /* desc of current UI */
    Call_Log_Node_t *CurrCallLog;                             /* selected call log in Call_Log_Call_List UI */
    Call_Log_Node_t *HisCallLog;                              /* selected call log in Call_Log_History_List UI */

    ACTIVITY_ID     ActBeforeCallList;                        /* activity ID before Call_Log_Call_List */
    ACTIVITY_ID     ActStatus;                                /* status activity ID */
#define UI_CALL_LOG_TIMER_LEN    2000                         /* 2 seconds */
    TIMER_ID        DisplayTimer;                             /* auto quit timer for status UI */
} Call_Log_Mng_t;

/**********************
* GLOBAL VARIABLES
**********************/
extern Call_Log_Mng_t g_Call_Log_Mng;

/**********************
* GLOBAL PROTOTYPES
**********************/
/*------ call_log_main ------*/
VOID Block_Set_State(UINT16 BlockId, UINT8 state);
extern INT16 Block_Alloc(VOID);
extern VOID Block_Free(UINT16 BlockId);
extern Call_Log_Node_t *Call_Log_Add(NV_Call_Log_Data_t *Data);
extern VOID Call_Log_Update_Cnt(CALL_LOG_TYPE Type, INT16 var);
extern UINT16 Call_Log_Get_Cnt(CALL_LOG_TYPE Type);
extern VOID Call_Log_Main(VOID);

/*------ call_log_ui ------*/
extern VOID Display_Call_Log_Menu(VOID);
extern VOID Display_Call_Log_No_Numbers(VOID);
extern VOID Display_Call_Log_No_Dialled_Numbers(VOID);
extern VOID Display_Call_Log_Call_List(CALL_LOG_TYPE Type, BOOL NavForward, ACTIVITY_ID SrcActId);
extern VOID Display_Call_Log_Opt(VOID);
extern VOID Display_Call_Log_History_List(VOID);
extern VOID Display_Call_Log_Delete_Number_Inquire(VOID);
extern VOID Display_Call_Log_Delete_Number_Done(VOID);
extern VOID Display_Call_Log_Delete_List(VOID);
extern VOID Display_Call_Log_Delete_List_Done(VOID);
extern VOID Call_Log_Call_List_Set_Label(UI_Normal_Container_List_Desc_t *pDesc);
extern VOID Call_Log_History_List_Set_Label(UI_Normal_Container_List_Desc_t *pDesc);

/*------ call_log_proc ------*/
// Call_Log_Menu
extern VOID Call_Log_Proc_Btn_Missed_Calls(lv_obj_t *Btn, lv_event_t e);
extern VOID Call_Log_Proc_Btn_Received_Calls(lv_obj_t *Btn, lv_event_t e);
extern VOID Call_Log_Proc_Btn_Dialled_Calls(lv_obj_t *Btn, lv_event_t e);
extern VOID Call_Log_Proc_Btn_Del_Recent_Calls(lv_obj_t *Btn, lv_event_t e);
extern UINT8 Call_Log_Proc_Key_Menu(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val);

// Call_Log_Call_List
extern UINT8 Call_Log_Proc_Key_Call_List(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val);

// Call_Log_Opt
extern VOID Call_Log_Proc_Btn_Call(lv_obj_t *Btn, lv_event_t e);
extern VOID Call_Log_Proc_Btn_Call_History(lv_obj_t *Btn, lv_event_t e);
extern VOID Call_Log_Proc_Btn_Send_Sms(lv_obj_t *Btn, lv_event_t e);
extern VOID Call_Log_Proc_Btn_Send_Number(lv_obj_t *Btn, lv_event_t e);
extern VOID Call_Log_Proc_Btn_Save_Number(lv_obj_t *Btn, lv_event_t e);
extern VOID Call_Log_Proc_Btn_Del_Number(lv_obj_t *Btn, lv_event_t e);
extern VOID Call_Log_Proc_Btn_Add_To_Scrn_List(lv_obj_t *Btn, lv_event_t e);
extern UINT8 Call_Log_Proc_Key_Opt(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val);

// Call_Log_History_List
UINT8 Call_Log_Proc_Key_History_List(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val);

// Call_Log_Delete_Number_Inquire
extern UINT8 Call_Log_Proc_Key_Delete_Number_Inquire(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val);

// Call_Log_Delete_List
extern VOID Call_Log_Proc_Btn_Del_All(lv_obj_t *Btn, lv_event_t e);
extern VOID Call_Log_Proc_Btn_Del_Missed(lv_obj_t *Btn, lv_event_t e);
extern VOID Call_Log_Proc_Btn_Del_Received(lv_obj_t *Btn, lv_event_t e);
extern VOID Call_Log_Proc_Btn_Del_Dialled(lv_obj_t *Btn, lv_event_t e);
extern UINT8 Call_Log_Proc_Key_Delete_List(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val);

// Call_Log_No_Dialled_Numbers
extern UINT8 Call_Log_Proc_Key_No_Dialled_Numbers(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val);

// Status
extern UINT8 Call_Log_Proc_Key_Status(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val);
extern VOID Call_Log_Proc_Status_Timeout(VOID* Para);

#ifdef __cplusplus
} /* extern "C" */
#endif

#endif /*CALL_LOG_GLOBAL_H*/
